import logging
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from selenium.webdriver.edge.service import Service as EdgeService

from config.config import *
# 根据配置项实现: 创建哪种浏览器对象, 使用哪种驱动管理方式, 是否开启无头模式


def get_driver():

    # 初始化driver
    driver = None

    # 判断浏览器类型
    if BROWSER_TYPE == "chrome":
        driver = get_chrome_driver()
    if BROWSER_TYPE == "edge":
        driver = get_edge_driver()

    return driver


def get_chrome_driver():
    # 不论用哪种驱动模式, 都需要先设置浏览器参数, 并判断是否开启无头模式
    options = webdriver.ChromeOptions()
    options.add_argument("--start-maximized")
    options.add_argument("--headless=new") if HEADLESS else None
    options.add_argument("--no-sandbox")          # 避免权限问题
    options.add_argument("--disable-dev-shm-usage")  # 共享内存不足时用
    options.add_argument("--disable-gpu")         # 服务器无 GPU

    # 判断使用哪种驱动(本地/远程)
    if DRIVER_TYPE == "local":
        service = Service(CHROME_DRIVER_PATH)
    else:
        service = Service(ChromeDriverManager().install())

    driver = webdriver.Chrome(service=service, options=options)
    logging.info("启动chrome浏览器成功")
    return driver


def get_edge_driver():
    # 不论用哪种驱动模式, 都需要先设置浏览器参数, 并判断是否开启无头模式
    options = webdriver.EdgeOptions()
    options.add_argument("--start-maximized")
    options.add_argument("--headless") if HEADLESS else None

    # 判断使用哪种驱动(本地/远程)
    if DRIVER_TYPE == "local":
        service = EdgeService(EDGE_DRIVER_PATH)
    else:
        service = EdgeService(EdgeChromiumDriverManager().install())

    driver = webdriver.Edge(service=service, options=options)
    logging.info("启动edge浏览器成功")
    return driver
